package com.yun.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yun.blog.bean.Aphorism;
import com.yun.blog.bean.Result;
import com.yun.blog.service.AphorismService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * title        : AphorismController
 * projectName  : dantouBlog-back
 * description  : 名言警句接口
 * author       : simple
 * date         : 2022/11/26下午9:47
 */

@Slf4j
@RestController
@RequestMapping("/aphorism")
@Api(tags = "名言警句接口")
public class AphorismController {

    @Resource
    AphorismService aphorismService;

    @RequestMapping(value = "/getOne", method = {RequestMethod.GET, RequestMethod.POST})
    @ApiOperation(value = "查询一条名言警句", notes = "id参数是可选的，如果传递了id，就查询不等于id的记录")
    public Result getOne(Integer id) {
        // 查询条件，如果传递了id，就查询不等于id的记录
        LambdaQueryWrapper<Aphorism> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.ne(id != null, Aphorism::getId, id);

        List<Aphorism> list = aphorismService.list(queryWrapper);
        if (list.size() > 0) {
            Random random = new Random();
            Aphorism aphorism = list.get(random.nextInt(list.size()));
            return Result.success().add("aphorism", aphorism);
        } else {
            return Result.fail("没有获取到其他名言警句");
        }
    }


    @PostMapping("/save")
    @ApiOperation("保存一条名言警句数据")
    public Result save(@RequestBody Aphorism aphorism) {
        try {
            aphorismService.save(aphorism);
        } catch (Exception e) {
            log.error("删除失败：错误信息为：{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
        return Result.success("保存成功！");
    }

    @GetMapping("/deleteAphorism")
    @ApiOperation("通过id删除一条名言警句数据")
    public Result deleteAphorism(Integer id) {
        try {
            aphorismService.removeById(id);
        } catch (Exception e) {
            log.error("删除失败：错误信息为：{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
        return Result.success("删除成功！");
    }

    @PostMapping("/deleteMany")
    @ApiOperation("通过一个id列表，删除多条名言警句数据")
    public Result deleteMany(@RequestBody ArrayList<Integer> ids) {
        log.info("ids:{}", ids);
        try {
            aphorismService.removeByIds(ids);
        } catch (Exception e) {
            log.error("删除失败：错误信息为：{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
        return Result.success("删除成功！");
    }

    @PostMapping("/updateAphorism")
    @ApiOperation("修改一条名言警句数据")
    public Result updateAphorism(@RequestBody Aphorism aphorism) {
        try {
            aphorismService.updateById(aphorism);
        } catch (Exception e) {
            log.error("修改失败：错误信息为：{}", e.getMessage());
            return Result.fail(e.getMessage());
        }
        return Result.success("修改成功！");
    }

    @GetMapping("/getAphorisms")
    @ApiOperation("得到分页中一页的多条名言警句")
    public Result getAphorisms(@RequestParam(defaultValue = "10") Integer pageSize,
                               @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "") String searchText) {
//        log.info("pageSize:{}", pageSize);
//        log.info("pageNum:{}", pageNum);
//        log.info("searchText:{}", searchText);
        Result success = Result.success();
        LambdaQueryWrapper<Aphorism> queryWrapper = Wrappers.lambdaQuery();
        // 如果有查询条件就查询名称一样的
        if (StringUtils.hasText(searchText)) {
            queryWrapper.like(Aphorism::getContent, searchText).or().like(Aphorism::getAuthor, searchText);
        }
        Page<Aphorism> page = aphorismService.page(new Page<>(pageNum, pageSize), queryWrapper);

        success.add("page", page);
        return success;
    }

}
